package com.yyx.demo.config;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.List;

public class MyMapperProxy implements InvocationHandler {


    private MyConfiguration myConfiguration;

    private MySqlsession mySqlsession;

    public MyMapperProxy(MyConfiguration myConfiguration, MySqlsession mySqlsession) {
        this.myConfiguration=myConfiguration;
        this.mySqlsession=mySqlsession;
    }

    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

        //读取bean
        MapperBean readeMapper = myConfiguration.readMapper("UserMybatis.xml");
        //是否是xml文件对应的接口
        if(!method.getDeclaringClass().getName().equals(readeMapper.getInterfaceName())){
            return null;
        }
        List<Function> list= readeMapper.getList();
        if(null!=list||0!=list.size()){
            for (Function function:list
            ) {
                //id是否和接口方法名一样
                if(method.getName().equals(function.getFunName())){
                    return mySqlsession.selectOne(function,String.valueOf(args[0]));
                }
            }

        }
        return null;
    }
}
